1 using System.Collections.Generic;
2 using
Assets.Scripts.Network;
3 using
UnityEngine;
4 using
UnityEngine.UI;
5
6 namespace
Assets.Scripts.UI
7 {
8     
public class Lobby : BasePanel
9     {
10         
public InputField playerName;
11         
public InputField createRoomName;
12
13         
public ScrollRect scrollRect;
14         
public GridLayoutGroup grid;
15
16         
public GameObject roomButtonPrefab;
17
18         
private readonly List<RoomButton> roomButtonList = new List<RoomButton>();
19
20         
public void OnBack()
21         {
22             Hide();
23             GameService.Quit();
24         }
25
26         
public void OnChangePlayerName()
27         {
28             NetworkService.PlayerName = playerName.text;
29         }
30         
31         
public void OnCreateRoom()
32         {
33             NetworkService.CreateRoom(createRoomName.text);
34         }
35
36         
public void OnJoinRandomRoom()
37         {
38             NetworkService.JoinRandomRoom();
39         }
40
41         
protected override void Show()
42         {
43             
base.Show();
44
45             playerName.text = NetworkService.PlayerName;
46             createRoomName.text = NetworkService.PlayerName;
47         }
48
49         
protected override void Start()
50         {
51             
base.Start();
52
53             NetworkService.OnConnectedToMasterSignal.AddListener(Show);
54             NetworkService.OnJoinedRoomSignal.AddListener(Hide);
55             NetworkService.OnDisconnectedFromMasterSignal.AddListener(Hide);
56         }
57
58         
protected override void OnDestroy()
59         {
60             
base.OnDestroy();
61
62             NetworkService.OnConnectedToMasterSignal.RemoveListener(Show);
63             NetworkService.OnJoinedRoomSignal.RemoveListener(Hide);
64             NetworkService.OnDisconnectedFromMasterSignal.RemoveListener(Hide);
65         }
66
67         
private RoomButton GetRoomButton()
68         {
69             GameObject aGO = (GameObject)Instantiate(roomButtonPrefab, Vector3.zero, Quaternion.identity);
70             aGO.transform.SetParent(grid.transform,
false);
71             
return aGO.GetComponent<RoomButton>();
72         }
73
74         
protected override void Update()
75         {
76             
base.Update();
77
78             
var roomList = NetworkService.GetRoomList();
79
80             
// Create more buttons if needed
81             
for (int i = 0; i < roomList.Count - roomButtonList.Count; i++)
82             {
83                 
var button = GetRoomButton();
84                 roomButtonList.Add(button);
85             }
86
87             
// Update all buttons
88             
for (int i = 0; i < roomButtonList.Count; i++)
89             {
90                 
if (i < roomList.Count)
91                 {
92                     roomButtonList[i].Init(roomList[i]);
93                     roomButtonList[i].gameObject.SetActive(
true);
94                 }
95                 
else
96                 {
97                     roomButtonList[i].gameObject.SetActive(
false);
98                 }
99             }
100         }
101     }
102 }


Create more buttons if needed

Update all buttons




Trò chơi Tic-Tac-Toe, game đánh caro full source code 53.487 lượt xem

Gõ tìm kiếm nhanh...